/**
 *
 */
package com.wedroid.api.mapper;

import java.util.Date;

import com.alibaba.fastjson.JSON;
import com.demo2do.core.utils.JsonUtils;
import com.wedroid.api.handler.ResourceHandler;
import com.wedroid.entity.message.Location;
import com.wedroid.entity.message.Message;
import com.wedroid.entity.message.MessageType;
import com.wedroid.entity.message.RecommendInfo;
import com.wedroid.entity.message.ShareInfo;

/**
 * @author lute
 */
public class MessageMapper {

    private Long id;

    private String msgId;

    private MessageType type;

    private Date messageTime;

    private String content;

    private ContacterMapper sender;

    private String senderName;

    private ContacterMapper receiver;

    private String receiverName;

    private ContacterMapper groupMember;

    private String groupMemberName;

    /**
     * @param message
     */
    public MessageMapper(Message message) {
        this.id = message.getId();
        this.msgId = message.getMsgId();
        this.type = message.getMessageTypeEnum();
        this.messageTime = message.getMessageTime();
        this.content = message.getContent();
        this.sender = new ContacterMapper(message.getSender());
        this.senderName = message.getSenderName();
        this.receiver = new ContacterMapper(message.getReceiver());
        this.receiverName = message.getReceiverName();
        if (message.getGroupMember() != null) {
            this.groupMember = new ContacterMapper(message.getGroupMember());
        }
        this.groupMemberName = message.getGroupMemberName();
    }

    /**
     * @return
     */
    public int getMessageTypeCode() {
        return type.getCode();
    }

    /**
     * @return
     */
    public String getSupportedDisplay() {
        return type.getSupportedDisplay();
    }

    /**
     * @param resourceHandler
     * @return
     */
    public MessageMapper resolveUrl(ResourceHandler resourceHandler) {
        switch(this.type) {
            case IMAGE:
                this.content = resourceHandler.resolveResourceUrl(this.content);
                break;
            case SHARE:
                ShareInfo shareInfo = JsonUtils.parse(this.content, ShareInfo.class);
                shareInfo.setThumbUrl(resourceHandler.resolveResourceUrl(shareInfo.getThumbUrl()));
                this.content = JsonUtils.toJsonString(shareInfo);
                break;
            case SHARECARD:
            case VERIFICATION:
                RecommendInfo recommendInfo = JsonUtils.parse(this.content, RecommendInfo.class);
                recommendInfo.setHeadImgUrl(resourceHandler.resolveResourceUrl(recommendInfo.getHeadImgUrl()));
                this.content = JsonUtils.toJsonString(recommendInfo);
                break;
            case TEXT:
                try {
                    Location location = JSON.parseObject(this.content, Location.class);
                    location.setMapUrl(resourceHandler.resolveResourceUrl(location.getMapUrl()));
                    this.content = JsonUtils.toJsonString(location);
                } catch (Exception e) {}
                break;
            default:
                break;
        }

        this.sender.resolveUrl(resourceHandler);
        this.receiver.resolveUrl(resourceHandler);
        if (this.groupMember != null) {
            this.groupMember.resolveUrl(resourceHandler);
        }
        return this;
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the msgId
     */
    public String getMsgId() {
        return msgId;
    }

    /**
     * @return the type
     */
    public MessageType getType() {
        return type;
    }

    /**
     * @return the messageTime
     */
    public Date getMessageTime() {
        return messageTime;
    }

    /**
     * @return the content
     */
    public String getContent() {
        return content;
    }

    /**
     * @return the sender
     */
    public ContacterMapper getSender() {
        return sender;
    }

    /**
     * @return the senderName
     */
    public String getSenderName() {
        return senderName;
    }

    /**
     * @return the receiver
     */
    public ContacterMapper getReceiver() {
        return receiver;
    }

    /**
     * @return the receiverName
     */
    public String getReceiverName() {
        return receiverName;
    }

    /**
     * @return the groupMember
     */
    public ContacterMapper getGroupMember() {
        return groupMember;
    }

    /**
     * @return the groupMemberName
     */
    public String getGroupMemberName() {
        return groupMemberName;
    }

}
